package com.cskaoyan.app.wxcontroller;

import com.cskaoyan.app.bean.bo.WxCouponBo;
import com.cskaoyan.app.bean.bo.adminbo.AdminListBo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.wxservice.WxCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * req:list,myList,selectList,receive,exchange
 * @author kiggy
 * @since 2023/01/01 19:22
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService couponService;

    @GetMapping("list")
    public BaseRespVo list(AdminListBo listBo){
        CommonData data = couponService.listCoupon(listBo);
        return BaseRespVo.ok(data);
    }

    @GetMapping("mylist")
    public BaseRespVo myList(AdminListBo listBo){
        CommonData data = couponService.myListCoupon(listBo);
        return BaseRespVo.ok(data);
    }

    @GetMapping("selectlist")
    public BaseRespVo selectList(WxCouponBo couponBo){
        CommonData data = couponService.selectList(couponBo);
        return BaseRespVo.ok(data);
    }


    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody WxCouponBo receiveBo){
        Integer status = couponService.receiveCoupon(receiveBo.getCouponId());
        if (status == 0){
            return BaseRespVo.ok();
        }else if (status == 740){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(740);
            baseRespVo.setErrmsg("优惠券已经领过了");
            return baseRespVo;
        }else {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(200);
            baseRespVo.setErrmsg("优惠券无法领取");
            return baseRespVo;
        }
    }

    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody WxCouponBo couponBo){
        Integer status = couponService.exchangeCoupon(couponBo);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if (status == 0){
            return BaseRespVo.ok();
        }else if (status == 742){
            baseRespVo.setErrno(740);
            baseRespVo.setErrmsg("优惠券不正确");
            return baseRespVo;
        }else {
            baseRespVo.setErrno(200);
            baseRespVo.setErrmsg("优惠券已领取");
            return baseRespVo;
        }
    }
}
